#include "stdafx.h"
#include "AuthDlg.h"

CAuthDlg::CAuthDlg(std::wstring *pstrLogin, std::wstring *pstrPassword):
	m_pstrLogin(pstrLogin),
	m_pstrPassword(pstrPassword)
{
}

LRESULT CAuthDlg::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	UNREFERENCED_PARAMETER(bHandled);
	UNREFERENCED_PARAMETER(lParam);
	UNREFERENCED_PARAMETER(wParam);
	UNREFERENCED_PARAMETER(uMsg);
	
	if (m_pstrLogin)
	{
		if (m_pstrLogin->size())
			SetDlgItemText(IDC_EDIT_LOGIN, m_pstrLogin->c_str());
	}
	else
	{
		::EnableWindow(GetDlgItem(IDC_EDIT_LOGIN), FALSE);
	}
	
	if (m_pstrPassword)
	{
		if (m_pstrPassword->size())
			SetDlgItemText(IDC_EDIT_PASSWORD, m_pstrPassword->c_str());
	}
	else
	{
		::EnableWindow(GetDlgItem(IDC_EDIT_PASSWORD), FALSE);
	}
	
	return TRUE;
}

LRESULT CAuthDlg::OnDone(WORD /* wNotifyCode */, WORD /* wID */, HWND /* hWndCtl */, BOOL& /* bHandled */)
{
	if (m_pstrLogin)
	{
		int len = (int)SendDlgItemMessage(IDC_EDIT_LOGIN, WM_GETTEXTLENGTH, 0, 0);
		
		if (len)
		{
			m_pstrLogin->resize(len);
			GetDlgItemText(IDC_EDIT_LOGIN, &m_pstrLogin->at(0), (int)(m_pstrLogin->size() + 1));
		}
	}
	
	if (m_pstrPassword)
	{
		int len = (int)SendDlgItemMessage(IDC_EDIT_PASSWORD, WM_GETTEXTLENGTH, 0, 0);
		
		if (len)
		{
			m_pstrPassword->resize(len);
			GetDlgItemText(IDC_EDIT_PASSWORD, &m_pstrPassword->at(0), (int)(m_pstrPassword->size() + 1));
		}
	}
	EndDialog(1);
	
	return TRUE;
}

LRESULT CAuthDlg::OnCancel(WORD /* wNotifyCode */, WORD /* wID */, HWND /* hWndCtl */, BOOL& /* bHandled */)
{
	EndDialog(0);
	
	return TRUE;
}

LRESULT CAuthDlg::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	UNREFERENCED_PARAMETER(bHandled);
	UNREFERENCED_PARAMETER(lParam);
	UNREFERENCED_PARAMETER(wParam);
	UNREFERENCED_PARAMETER(uMsg);
	
	EndDialog(0);
	
	return TRUE;
}